package adminMenuService

import (
	"encoding/json"

	"gitee.com/krio/ginc/example/core/exerr"
	exampleDtoApiPbAdminMenu "gitee.com/krio/ginc/example/dto/api/pb/admin/menu"
	adminMenuDao "gitee.com/krio/ginc/example/internal/admin/menu/dao"
	adminMenuModel "gitee.com/krio/ginc/example/internal/admin/menu/model"
	commonEntry "gitee.com/krio/ginc/example/internal/common/entry/common"
	commonModel "gitee.com/krio/ginc/example/internal/common/model/common"
	"github.com/gin-gonic/gin"
)

// AdminMenuList 导航栏
func AdminMenuList(ctx *gin.Context, req *exampleDtoApiPbAdminMenu.AdminMenuListReq) (*exampleDtoApiPbAdminMenu.AdminMenuListResp, error) {
	resp := &exampleDtoApiPbAdminMenu.AdminMenuListResp{}
	search := struct {
		Keyword string `json:"keyword"`
	}{}
	if req.Keywords != "" {
		err := json.Unmarshal([]byte(req.Keywords), &search)
		if err != nil {
			return nil, err
		}
	}
	dao := &adminMenuDao.AdminMenuDao{}
	list, total, err := dao.MenuList(ctx, &commonEntry.ListCommonReq{
		Page:    req.Page,
		Limit:   req.Limit,
		Keyword: search.Keyword,
		OrderBy: []*commonEntry.OrderBy{
			{
				IsDesc: true,
				Field:  "id",
			},
		},
	})
	if err != nil {
		return nil, err
	}
	var data []*exampleDtoApiPbAdminMenu.AdminMenuInfo
	for _, v := range list {
		data = append(data, formatDetail(v))
	}
	resp.Total = total
	resp.List = data
	return resp, nil
}

// AdminMenuDetail 详情
func AdminMenuDetail(ctx *gin.Context, req *exampleDtoApiPbAdminMenu.AdminMenuDetailReq) (*exampleDtoApiPbAdminMenu.AdminMenuInfo, error) {
	dao := &adminMenuDao.AdminMenuDao{}
	detail, has, err := dao.MenuDetail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, exerr.DataNotFound
	}

	return formatDetail(detail), nil
}

// AdminMenuAdd 新增
func AdminMenuAdd(ctx *gin.Context, req *exampleDtoApiPbAdminMenu.AdminMenuInfo) (*exampleDtoApiPbAdminMenu.AdminMenuAddResp, error) {
	one := &adminMenuModel.AdminMenu{
		Pid:     req.Pid,
		Title:   req.Title,
		Href:    req.Href,
		Icon:    req.Icon,
		Image:   req.Image,
		Target:  req.Target,
		AfterId: req.AfterId,
		Status:  commonModel.DataStatus(req.Status),
	}
	dao := &adminMenuDao.AdminMenuDao{}
	id, err := dao.MenuAdd(ctx, one)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminMenu.AdminMenuAddResp{
		Id: id,
	}, nil
}

// AdminMenuUpdate 修改
func AdminMenuUpdate(ctx *gin.Context, req *exampleDtoApiPbAdminMenu.AdminMenuInfo) (*exampleDtoApiPbAdminMenu.AdminMenuAddResp, error) {
	dao := &adminMenuDao.AdminMenuDao{}
	_, err := dao.MenuUpdate(ctx, &adminMenuModel.AdminMenu{
		Basic:   commonModel.Basic{ID: req.Id},
		Pid:     req.Pid,
		Title:   req.Title,
		Href:    req.Href,
		Icon:    req.Icon,
		Image:   req.Image,
		Target:  req.Target,
		AfterId: req.AfterId,
		Status:  commonModel.DataStatus(req.Status),
	})
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminMenu.AdminMenuAddResp{}, nil
}

// AdminMenuDelete 删除
func AdminMenuDelete(ctx *gin.Context, req *exampleDtoApiPbAdminMenu.AdminMenuDeleteReq) (*exampleDtoApiPbAdminMenu.AdminMenuDeleteResp, error) {
	dao := &adminMenuDao.AdminMenuDao{}
	err := dao.MenuDelete(ctx, req.Ids)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminMenu.AdminMenuDeleteResp{}, nil
}

// 数据装换
func formatDetail(v *adminMenuModel.AdminMenu) *exampleDtoApiPbAdminMenu.AdminMenuInfo {
	return &exampleDtoApiPbAdminMenu.AdminMenuInfo{
		Id:         v.ID,
		Pid:        v.Pid,
		Title:      v.Title,
		Href:       v.Href,
		Icon:       v.Icon,
		Image:      v.Image,
		Target:     v.Target,
		Status:     int32(v.Status),
		AfterId:    v.AfterId,
		CreateTime: v.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdateTime: v.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
}
